global placebo = "no"
*global placebo = "yes"

global lag = 1
global lead = 3

local lowhigh "low high"  //Low means 25(or 10)+, high means 50 (or 25)+
foreach small of local lowhigh {

if "`small'"=="low" local cut100 = .35
if "`small'"=="low" local cut50 = .15
if "`small'"=="low" local cut25 = 0.001
if "`small'"=="high" local cut100 = .50
if "`small'"=="high" local cut50 = .25
if "`small'"=="high" local cut25 = .05

	foreach cutoff of numlist 0.25 0.5 0.75 1  {


		global cutoff = `cutoff'
		use "${data1}state_panel_mw_poverty_84_w", clear
		keep if year<=2013
		
		//Merge in Controls (EITC and stuff)
		preserve
		use "${data2}aca_expansion_eitc.dta", clear
		replace year = year+1 //Match policy at t-1 to t
		tempfile controls
		save `controls'
		restore
		merge 1:1 state_fips year using `controls', keep(1 3) nogen

		sort state_fips year
		by state_fips: gen mw_change_nom = mw - mw[_n-1]
		replace mw_change_nom=0 if mw_change_nom==.

		*gen tryear=year if mw_change>=$cutoff
		gen tryear=year if mw_change_nom>=$cutoff

		//Ignore "treatment" if "treatment" occured the year before
		sort state_fips year
		by state_fips: gen tryear_lag=tryear[_n-1]
		gen tryear_eff = tryear
		replace tryear_eff=. if tryear-tryear_lag==1
		drop tryear_lag

		sort tryear_eff state_fips 
		gen eventid=_n if tryear_eff!=.

		qui sum eventid 
		local max = r(max)

		//Delete all files
		local datafiles: dir "${synth}" files "weight*.dta"
		foreach datafile of local datafiles {
			erase "${synth}`datafile'"
		}

		local datafiles: dir "${synth}" files "synth*.dta"
		foreach datafile of local datafiles {
			erase "${synth}`datafile'"
		}
		
		if "$placebo"=="yes" {
			local datafiles: dir "${synth}" files "coef*.dta"
			foreach datafile of local datafiles {
				erase "${synth}`datafile'"
			}
		}
		
		local countevent = 0
		foreach i of numlist 1/`max' {
			preserve
			
			timer clear
			timer on 1
			
			dis `i' 
			qui {
				** Figure out Treatment State + Year
				sum tryear if eventid==`i'
				global tryear = r(mean)
				
				sum state_fips if eventid==`i'
				global trfips = r(mean)
				

				***Sample Cuts
				
				//Keep donor states w/o "treatment" 
				local lead = $lead
				local lag = $lag
				keep if inrange(year, $tryear -`lead', $tryear + `lag') 
				if `cutoff'==0.1 {
					drop if mw_change_nom >=`cut10' & state_fips!=$trfips
				} 
				else if `cutoff'==0.25 {
					drop if mw_change_nom >=`cut25' & state_fips!=$trfips
				}
				else if `cutoff'==0.5 {
					drop if mw_change_nom >=`cut50' & state_fips!=$trfips
				} 
				else {
					drop if mw_change_nom >=`cut100' & state_fips!=$trfips
				}
				
				//Keep "treatment" states only if NO pre-treat min wage increase
				drop if inrange(tryear, $tryear -`lead', $tryear - 1)  & state_fips==$trfips
				bys state_fips: gen count=_N
				if `lag'==1 {
					keep if (count==`lead'+`lag'+1) | (count==`lead'+1 & ${tryear}==2013)  
				}
				
				if `lag'==2 {
					keep if (count==`lead'+`lag'+1) | (count==`lead'+1 & ${tryear}==2013) |  (count==`lead'+2 & ${tryear}==2012)   
				}
				
				//Ignore cases where there are NO treatment state
				sum state_fips if state_fips==$trfips
				local obs =r(N)
				
				if `obs' > 0 {
					
					//Count Number of Donors in the Divison
					qui sum division if eventid==`i'
					local trdiv = r(mean)
					bys state_fips: gen stateid=1 if _n==1 & division==`trdiv'
					egen count_div= count(stateid)
					sum count_div
					local count_div = r(mean)
					
							
					//Ignore cases where there are only ONE donor state
					cap drop stateid
					bys state_fips: gen stateid=1 if _n==1 
					egen count_donor= count(stateid)
					sum count_donor
					local count_donor = r(mean) 	
				
					**Create Pre-Treat Mean
					gen preoutcome = under_100 if year<$tryear 
					bys state_fips: egen pretreatmean = mean(preoutcome)

				
					***Run Synthetic Control
					if `count_donor' > 2  {						
						xtset state_fips year
						local pre=$tryear-`lead'
						local before=$tryear-1
						local post=$tryear+`lag'
						if `post' > 2020 { 
							local post=2020
						}
						
						
						local control1=""
						foreach t of numlist `pre'/`before' {
							local control1 = "`control1'" + "under_100(`t') "
						}
						
						local control2 = "pretreatmean "
						foreach var of varlist hpi ur_high_edu wage_high_edu {	
							foreach t of numlist `pre' $tryear `post' {
								local control2 = "`control2'" + "`var'(`t') "
							}
						}
						
						local control3 = "pretreatmean "
						foreach var of varlist ur gdppc {	
							foreach t of numlist `pre' $tryear `post' {
								local control3 = "`control3'" + "`var'(`t') "
							}
						}
						
						
						local control4 = "`control2'"
						foreach var of varlist eitc tanf4 {	
							foreach t of numlist `pre' $tryear `post' {
								local control4 = "`control4'" + "`var'(`t') "
							}
						}	
						
						
						dis "`control1'"
						dis "`control2'"
						dis "`control3'"
						dis "`control4'"
						
						tempfile save1
						synth under_100	`control1', trunit($trfips) trperiod($tryear) keep(`save1')
						if "$placebo" == "yes" runplacebo "`control1'" `i' 1

						tempfile save2
						synth under_100	`control2', trunit($trfips) trperiod($tryear) keep(`save2')
						if "$placebo" == "yes" runplacebo "`control2'" `i' 2

						tempfile save3
						synth under_100 `control3', trunit($trfips) trperiod($tryear) keep(`save3')
						if "$placebo" == "yes" runplacebo "`control3'" `i' 3
						
						tempfile save4
						synth under_100 `control4', trunit($trfips) trperiod($tryear) keep(`save4')
						if "$placebo" == "yes" runplacebo "`control4'" `i' 4


						foreach j of numlist 1/4 {
							use `save`j'', clear
							*keep _Y_treated _Y_synthetic _time
							*keep if   _time!=.
							gen treat_year = $tryear
							gen treat_fips=$trfips
							gen no_donor = `count_donor' - 1
							gen no_donor_div = `count_div' -1
							gen model_no=`j'
							save "${synth}synth`j'_`i'.dta", replace
							
							/*keep if  _time >= $tryear
							gen att = _Y_treated - _Y_synthetic
							collapse (mean) att, by(treat_*)
							gen model_no = `j'
							save "${synth}coef`j'_`i'.dta", replace*/

						}
						
						local countevent = `countevent' + 1
					}
				}
			}		
			
			restore
		}
		
		timer off 1
		timer list


		**Get Synth & Actual Time Series
		foreach i of numlist 1/4 {
			clear
			local datafiles: dir "${synth}" files "synth`i'*.dta"
			foreach datafile of local datafiles {
				qui append using "${synth}`datafile'"
			}
			gen model= `i'
			tempfile synthactual`i'
			save `synthactual`i''
		}
		use `synthactual1'
		append using `synthactual2'
		append using `synthactual3'
		append using `synthactual4'
		
		local save = $cutoff*100
		export delimited using "${data2}synth/synth_timeseries_`save'_${lead}_${lag}_`small'_8413", replace
		
		**Get ATT & P-Val & Individual Weights
		if "$placebo" == "yes" {
			local datafiles: dir "${synth}" files "coef*.dta"
			clear
			foreach datafile of local datafiles {
				qui append using "${synth}`datafile'"
			}
			
			export delimited using "${tab}synth_att_`save'_${lead}_${lag}_`small'_8413", replace
		}
		
	}
}
